package edu.yuhf.web.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

@Controller
@RequestMapping("/ajaxdemo")
public class AjaxController {

	@RequestMapping("/forward")
	public String forward(){
		return "views/ajax/ajaxdemo";
	}
	
	@ResponseBody
	@RequestMapping(value="/doAjax1",produces = "text/html;charset=UTF-8")
	public String doAjax1(){
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("message", "ajax返回数据！");
		map.put("result", "success");
		String jsonString=JSONObject.toJSONString(map);
		return jsonString;
	}
	
	@RequestMapping("/doAjax2")
	public String doAjax2(HttpServletResponse response) throws IOException{
		response.setCharacterEncoding("utf-8");
		PrintWriter out=response.getWriter();
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("message", "ajax返回数据！");
		map.put("result", "success");
		String jsonString=JSONObject.toJSONString(map);
		out.print(jsonString);
		out.flush();
		out.close();
		return null;
	}
}
